package com.gmrz.uap.common;

import com.gmrz.uap.cfg.SystemConst;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class DbConst {
    private static final Logger LOG = LogManager.getLogger(DbConst.class);

    public static final String TABLE_SEPARATOR = "t_";

    /**************************** 此部分代码由主方法生成 *******************************/
    public static final String TABLE_QRTZ_BLOB_TRIGGERS = "qrtz_blob_triggers";

    public static final String TABLE_QRTZ_CALENDARS = "qrtz_calendars";

    public static final String TABLE_QRTZ_CRON_TRIGGERS = "qrtz_cron_triggers";

    public static final String TABLE_QRTZ_FIRED_TRIGGERS = "qrtz_fired_triggers";

    public static final String TABLE_QRTZ_JOB_DETAILS = "qrtz_job_details";

    public static final String TABLE_QRTZ_LOCKS = "qrtz_locks";

    public static final String TABLE_QRTZ_PAUSED_TRIGGER_GRPS = "qrtz_paused_trigger_grps";

    public static final String TABLE_QRTZ_SCHEDULER_STATE = "qrtz_scheduler_state";

    public static final String TABLE_QRTZ_SIMPLE_TRIGGERS = "qrtz_simple_triggers";

    public static final String TABLE_QRTZ_SIMPROP_TRIGGERS = "qrtz_simprop_triggers";

    public static final String TABLE_QRTZ_TRIGGERS = "qrtz_triggers";

    public static final String TABLE_AUTH_METADATA = "t_auth_metadata";
    public static final String TABLE_AUTH_METADATA_COLUMN_METADATA_ID = "metadata_id";
    public static final String TABLE_AUTH_METADATA_COLUMN_AAID = "aaid";
    public static final String TABLE_AUTH_METADATA_COLUMN_AUTH_VERSION = "auth_version";
    public static final String TABLE_AUTH_METADATA_COLUMN_DESCRIPTION = "description";
    public static final String TABLE_AUTH_METADATA_COLUMN_JSON_DATA = "json_data";
    public static final String TABLE_AUTH_METADATA_COLUMN_STATUS = "status";
    public static final String TABLE_AUTH_METADATA_COLUMN_CREATETS = "createts";
    public static final String TABLE_AUTH_METADATA_COLUMN_UPDATETS = "updatets";
    public static final String TABLE_AUTH_METADATA_COLUMN_AUTH_TYPE = "auth_type";

    public static final String TABLE_AUTHENTICATORS = "t_authenticators";
    public static final String TABLE_AUTHENTICATORS_COLUMN_AUTH_ID = "auth_id";
    public static final String TABLE_AUTHENTICATORS_COLUMN_METADATA_ID = "metadata_id";
    public static final String TABLE_AUTHENTICATORS_COLUMN_USER_NAME = "user_name";
    public static final String TABLE_AUTHENTICATORS_COLUMN_CUST_NO = "cust_no";
    public static final String TABLE_AUTHENTICATORS_COLUMN_AAID = "aaid";
    public static final String TABLE_AUTHENTICATORS_COLUMN_KEYID = "keyid";
    public static final String TABLE_AUTHENTICATORS_COLUMN_PUBKEY = "pubkey";
    public static final String TABLE_AUTHENTICATORS_COLUMN_KEY_FORMAT = "key_format";
    public static final String TABLE_AUTHENTICATORS_COLUMN_SIGN_COUNTER = "sign_counter";
    public static final String TABLE_AUTHENTICATORS_COLUMN_REG_COUNTER = "reg_counter";
    public static final String TABLE_AUTHENTICATORS_COLUMN_DEVICE_ID = "device_id";
    public static final String TABLE_AUTHENTICATORS_COLUMN_STATUS = "status";
    public static final String TABLE_AUTHENTICATORS_COLUMN_PNG_CHAR = "png_char";
    public static final String TABLE_AUTHENTICATORS_COLUMN_CREATETS = "createts";
    public static final String TABLE_AUTHENTICATORS_COLUMN_UPDATETS = "updatets";
    public static final String TABLE_AUTHENTICATORS_COLUMN_TENANT_ID = "tenant_id";
    public static final String TABLE_AUTHENTICATORS_COLUMN_TRANS_TYPE = "trans_type";
    public static final String TABLE_AUTHENTICATORS_COLUMN_AUTH_TYPE = "auth_type";
    public static final String TABLE_AUTHENTICATORS_COLUMN_UVI = "uvi";
    public static final String TABLE_AUTHENTICATORS_COLUMN_UVS = "uvs";
    public static final String TABLE_AUTHENTICATORS_COLUMN_MOBILE = "mobile";
    public static final String TABLE_AUTHENTICATORS_COLUMN_RF1 = "rf1";
    public static final String TABLE_AUTHENTICATORS_COLUMN_RF2 = "rf2";

    public static final String TABLE_DEVICES = "t_devices";
    public static final String TABLE_DEVICES_COLUMN_DEV_ID = "dev_id";
    public static final String TABLE_DEVICES_COLUMN_USER_NAME = "user_name";
    public static final String TABLE_DEVICES_COLUMN_ALIAS_NAME = "alias_name";
    public static final String TABLE_DEVICES_COLUMN_TRANS_TYPE = "trans_type";
    public static final String TABLE_DEVICES_COLUMN_AUTH_TYPE = "auth_type";
    public static final String TABLE_DEVICES_COLUMN_DEVICE_ID = "device_id";
    public static final String TABLE_DEVICES_COLUMN_REQUEST = "request";
    public static final String TABLE_DEVICES_COLUMN_DEVICE_TYPE = "device_type";
    public static final String TABLE_DEVICES_COLUMN_DEVICE_NAME = "device_name";
    public static final String TABLE_DEVICES_COLUMN_OS_VERSION = "os_version";
    public static final String TABLE_DEVICES_COLUMN_OS_TYPE = "os_type";
    public static final String TABLE_DEVICES_COLUMN_IMEI = "imei";
    public static final String TABLE_DEVICES_COLUMN_MAC = "mac";
    public static final String TABLE_DEVICES_COLUMN_STATUS = "status";
    public static final String TABLE_DEVICES_COLUMN_CREATETS = "createts";
    public static final String TABLE_DEVICES_COLUMN_UPDATETS = "updatets";
    public static final String TABLE_DEVICES_COLUMN_TENANT_ID = "tenant_id";

    public static final String TABLE_DICT = "t_dict";
    public static final String TABLE_DICT_COLUMN_DICT_ID = "dict_id";
    public static final String TABLE_DICT_COLUMN_DT_ID = "dt_id";
    public static final String TABLE_DICT_COLUMN_DICT_NAME = "dict_name";
    public static final String TABLE_DICT_COLUMN_ORDER_NUMBER = "order_number";

    public static final String TABLE_DICT_TYPE = "t_dict_type";
    public static final String TABLE_DICT_TYPE_COLUMN_DT_ID = "dt_id";
    public static final String TABLE_DICT_TYPE_COLUMN_DT_NAME = "dt_name";
    public static final String TABLE_DICT_TYPE_COLUMN_ORDER_NUMBER = "order_number";

    public static final String TABLE_EQUIPMENT = "t_equipment";
    public static final String TABLE_EQUIPMENT_COLUMN_EQU_ID = "equ_id";
    public static final String TABLE_EQUIPMENT_COLUMN_VEN_ID = "ven_id";
    public static final String TABLE_EQUIPMENT_COLUMN_EQU_MODEL = "equ_model";
    public static final String TABLE_EQUIPMENT_COLUMN_STATUS = "status";
    public static final String TABLE_EQUIPMENT_COLUMN_CREATETS = "createts";

    public static final String TABLE_EQUIPMENT_AUTH = "t_equipment_auth";
    public static final String TABLE_EQUIPMENT_AUTH_COLUMN_EQU_ID = "equ_id";
    public static final String TABLE_EQUIPMENT_AUTH_COLUMN_METADATA_ID = "metadata_id";

    public static final String TABLE_EQUIPMENT_FORBID = "t_equipment_forbid";
    public static final String TABLE_EQUIPMENT_FORBID_COLUMN_EA_ID = "ea_id";
    public static final String TABLE_EQUIPMENT_FORBID_COLUMN_EQU_ID = "equ_id";
    public static final String TABLE_EQUIPMENT_FORBID_COLUMN_TENANT_ID = "tenant_id";
    public static final String TABLE_EQUIPMENT_FORBID_COLUMN_VEN_ID = "ven_id";

    public static final String TABLE_FACETS = "t_facets";
    public static final String TABLE_FACETS_COLUMN_FACET_ID = "facet_id";
    public static final String TABLE_FACETS_COLUMN_FACET_NAME = "facet_name";
    public static final String TABLE_FACETS_COLUMN_FACET_TYPE = "facet_type";
    public static final String TABLE_FACETS_COLUMN_CREATETS = "createts";
    public static final String TABLE_FACETS_COLUMN_UPDATETS = "updatets";
    public static final String TABLE_FACETS_COLUMN_AUTH_CODE = "auth_code";
    public static final String TABLE_FACETS_COLUMN_TENANT_ID = "tenant_id";

    public static final String TABLE_PLUGIN_ARGUMENT = "t_plugin_argument";
    public static final String TABLE_PLUGIN_ARGUMENT_COLUMN_PA_ID = "pa_id";
    public static final String TABLE_PLUGIN_ARGUMENT_COLUMN_SP_ID = "sp_id";
    public static final String TABLE_PLUGIN_ARGUMENT_COLUMN_CONFIG_CNAME = "config_cname";
    public static final String TABLE_PLUGIN_ARGUMENT_COLUMN_CONFIG_VALUE = "config_value";
    public static final String TABLE_PLUGIN_ARGUMENT_COLUMN_DES = "des";
    public static final String TABLE_PLUGIN_ARGUMENT_COLUMN_CREATETS = "createts";
    public static final String TABLE_PLUGIN_ARGUMENT_COLUMN_UPDATETS = "updatets";
    public static final String TABLE_PLUGIN_ARGUMENT_COLUMN_ORDER_NUMBER = "order_number";

    public static final String TABLE_POLICY = "t_policy";
    public static final String TABLE_POLICY_COLUMN_PL_ID = "pl_id";
    public static final String TABLE_POLICY_COLUMN_POLICY_NAME = "policy_name";
    public static final String TABLE_POLICY_COLUMN_AUTH_TYPE = "auth_type";
    public static final String TABLE_POLICY_COLUMN_TRANS_TYPE = "trans_type";
    public static final String TABLE_POLICY_COLUMN_TENANT_ID = "tenant_id";
    public static final String TABLE_POLICY_COLUMN_CREATETS = "createts";

    public static final String TABLE_POLICY_AUTH = "t_policy_auth";
    public static final String TABLE_POLICY_AUTH_COLUMN_PL_ID = "pl_id";
    public static final String TABLE_POLICY_AUTH_COLUMN_METADATA_ID = "metadata_id";

    public static final String TABLE_RESOURCE = "t_resource";
    public static final String TABLE_RESOURCE_COLUMN_RESOURCE_ID = "resource_id";
    public static final String TABLE_RESOURCE_COLUMN_RESOURCE_NAME = "resource_name";
    public static final String TABLE_RESOURCE_COLUMN_PID = "pid";
    public static final String TABLE_RESOURCE_COLUMN_URL = "url";
    public static final String TABLE_RESOURCE_COLUMN_RESOURCE_TYPE = "resource_type";
    public static final String TABLE_RESOURCE_COLUMN_ICON = "icon";
    public static final String TABLE_RESOURCE_COLUMN_DES = "des";
    public static final String TABLE_RESOURCE_COLUMN_ORDER_NUMBER = "order_number";
    public static final String TABLE_RESOURCE_COLUMN_CREATETS = "createts";
    public static final String TABLE_RESOURCE_COLUMN_UPDATETS = "updatets";

    public static final String TABLE_RESOURCE_ROLE = "t_resource_role";
    public static final String TABLE_RESOURCE_ROLE_COLUMN_USER_ID = "user_id";
    public static final String TABLE_RESOURCE_ROLE_COLUMN_ROLE_ID = "role_id";
    public static final String TABLE_RESOURCE_ROLE_COLUMN_RESOURCE_ID = "resource_id";
    public static final String TABLE_RESOURCE_ROLE_COLUMN_CHECK_STATE = "check_state";

    public static final String TABLE_ROLE = "t_role";
    public static final String TABLE_ROLE_COLUMN_ROLE_ID = "role_id";
    public static final String TABLE_ROLE_COLUMN_ROLE_NAME = "role_name";
    public static final String TABLE_ROLE_COLUMN_ICON = "icon";
    public static final String TABLE_ROLE_COLUMN_PID = "pid";
    public static final String TABLE_ROLE_COLUMN_DES = "des";
    public static final String TABLE_ROLE_COLUMN_ORDER_NUMBER = "order_number";
    public static final String TABLE_ROLE_COLUMN_CREATETS = "createts";
    public static final String TABLE_ROLE_COLUMN_UPDATETS = "updatets";

    public static final String TABLE_SERVICE_PLUGIN = "t_service_plugin";
    public static final String TABLE_SERVICE_PLUGIN_COLUMN_SP_ID = "sp_id";
    public static final String TABLE_SERVICE_PLUGIN_COLUMN_PLUGIN_NAME = "plugin_name";
    public static final String TABLE_SERVICE_PLUGIN_COLUMN_CLASS_NAME = "class_name";
    public static final String TABLE_SERVICE_PLUGIN_COLUMN_DES = "des";
    public static final String TABLE_SERVICE_PLUGIN_COLUMN_AAID = "aaid";

    public static final String TABLE_STASTIC = "t_stastic";
    public static final String TABLE_STASTIC_COLUMN_STA_ID = "sta_id";
    public static final String TABLE_STASTIC_COLUMN_CURR_YEAR = "curr_year";
    public static final String TABLE_STASTIC_COLUMN_STA_TYPE = "sta_type";
    public static final String TABLE_STASTIC_COLUMN_TENANT_ID = "tenant_id";
    public static final String TABLE_STASTIC_COLUMN_AUTH_TYPE = "auth_type";
    public static final String TABLE_STASTIC_COLUMN_JAN_TOTAL = "jan_total";
    public static final String TABLE_STASTIC_COLUMN_FEB_TOTAL = "feb_total";
    public static final String TABLE_STASTIC_COLUMN_MAR_TOTAL = "mar_total";
    public static final String TABLE_STASTIC_COLUMN_APR_TOTAL = "apr_total";
    public static final String TABLE_STASTIC_COLUMN_MAY_TOTAL = "may_total";
    public static final String TABLE_STASTIC_COLUMN_JUN_TOTAL = "jun_total";
    public static final String TABLE_STASTIC_COLUMN_JUL_TOTAL = "jul_total";
    public static final String TABLE_STASTIC_COLUMN_AUG_TOTAL = "aug_total";
    public static final String TABLE_STASTIC_COLUMN_SEP_TOTAL = "sep_total";
    public static final String TABLE_STASTIC_COLUMN_OCT_TOTAL = "oct_total";
    public static final String TABLE_STASTIC_COLUMN_NOV_TOTAL = "nov_total";
    public static final String TABLE_STASTIC_COLUMN_DEC_TOTAL = "dec_total";

    public static final String TABLE_TENANT = "t_tenant";
    public static final String TABLE_TENANT_COLUMN_TAN_ID = "tan_id";
    public static final String TABLE_TENANT_COLUMN_TENANT_ID = "tenant_id";
    public static final String TABLE_TENANT_COLUMN_ALIAS_NAME = "alias_name";
    public static final String TABLE_TENANT_COLUMN_STATUS = "status";
    public static final String TABLE_TENANT_COLUMN_CREATETS = "createts";
    public static final String TABLE_TENANT_COLUMN_UPDATETS = "updatets";

    public static final String TABLE_TENANT_CONFIG = "t_tenant_config";
    public static final String TABLE_TENANT_CONFIG_COLUMN_TC_ID = "tc_id";
    public static final String TABLE_TENANT_CONFIG_COLUMN_CONFIG_CNAME = "config_cname";
    public static final String TABLE_TENANT_CONFIG_COLUMN_CONFIG_ENAME = "config_ename";
    public static final String TABLE_TENANT_CONFIG_COLUMN_CONFIG_VALUE = "config_value";
    public static final String TABLE_TENANT_CONFIG_COLUMN_DATA_TYPE = "data_type";
    public static final String TABLE_TENANT_CONFIG_COLUMN_REGEX = "regex";
    public static final String TABLE_TENANT_CONFIG_COLUMN_ERROR_MSG = "error_msg";
    public static final String TABLE_TENANT_CONFIG_COLUMN_CREATETS = "createts";
    public static final String TABLE_TENANT_CONFIG_COLUMN_TENANT_ID = "tenant_id";

    public static final String TABLE_UAS_CONFIG = "t_uas_config";
    public static final String TABLE_UAS_CONFIG_COLUMN_CONFIG_ID = "config_id";
    public static final String TABLE_UAS_CONFIG_COLUMN_CONFIG_CNAME = "config_cname";
    public static final String TABLE_UAS_CONFIG_COLUMN_CONFIG_ENAME = "config_ename";
    public static final String TABLE_UAS_CONFIG_COLUMN_CONFIG_VALUE = "config_value";
    public static final String TABLE_UAS_CONFIG_COLUMN_CONFIG_TYPE = "config_type";
    public static final String TABLE_UAS_CONFIG_COLUMN_CONFIG_USER_DEFINED = "config_user_defined";
    public static final String TABLE_UAS_CONFIG_COLUMN_CONFIG_DEFAULT_VALUE = "config_default_value";
    public static final String TABLE_UAS_CONFIG_COLUMN_CREATETS = "createts";
    public static final String TABLE_UAS_CONFIG_COLUMN_UPDATETS = "updatets";
    public static final String TABLE_UAS_CONFIG_COLUMN_ORDER_NUMBER = "order_number";
    public static final String TABLE_UAS_CONFIG_COLUMN_DATA_TYPE = "data_type";
    public static final String TABLE_UAS_CONFIG_COLUMN_REGEX = "regex";
    public static final String TABLE_UAS_CONFIG_COLUMN_ERROR_MSG = "error_msg";

    public static final String TABLE_USER = "t_user";
    public static final String TABLE_USER_COLUMN_USER_ID = "user_id";
    public static final String TABLE_USER_COLUMN_USER_NAME = "user_name";
    public static final String TABLE_USER_COLUMN_PWD = "pwd";
    public static final String TABLE_USER_COLUMN_ICON = "icon";
    public static final String TABLE_USER_COLUMN_EMAIL = "email";
    public static final String TABLE_USER_COLUMN_PHONE = "phone";
    public static final String TABLE_USER_COLUMN_DES = "des";
    public static final String TABLE_USER_COLUMN_CREATETS = "createts";
    public static final String TABLE_USER_COLUMN_UPDATETS = "updatets";
    public static final String TABLE_USER_COLUMN_TENANT_ID = "tenant_id";

    public static final String TABLE_USER_ROLE = "t_user_role";
    public static final String TABLE_USER_ROLE_COLUMN_USER_ID = "user_id";
    public static final String TABLE_USER_ROLE_COLUMN_ROLE_ID = "role_id";

    public static final String TABLE_VENDOR = "t_vendor";
    public static final String TABLE_VENDOR_COLUMN_VEN_ID = "ven_id";
    public static final String TABLE_VENDOR_COLUMN_VENDOR_NAME = "vendor_name";
    public static final String TABLE_VENDOR_COLUMN_VENDOR_ALIAS = "vendor_alias";

    public static final String TABLE_VENDOR_ALLOW = "t_vendor_allow";
    public static final String TABLE_VENDOR_ALLOW_COLUMN_VF_ID = "vf_id";
    public static final String TABLE_VENDOR_ALLOW_COLUMN_VEN_ID = "ven_id";
    public static final String TABLE_VENDOR_ALLOW_COLUMN_TENANT_ID = "tenant_id";

    public static final String TABLE_WHITE_LIST = "t_white_list";
    public static final String TABLE_WHITE_LIST_COLUMN_WL_ID = "wl_id";
    public static final String TABLE_WHITE_LIST_COLUMN_VISIT_ADDRESS = "visit_address";
    public static final String TABLE_WHITE_LIST_COLUMN_TENANT_ID = "tenant_id";


    public static final String TABLE_FIDO_LOG = "t_fido_log";
    public static final String TABLE_FIDO_LOG_COLUMN_FL_ID = "fl_id";
    public static final String TABLE_FIDO_LOG_COLUMN_TRANSEQ = "transeq";
    public static final String TABLE_FIDO_LOG_COLUMN_TENANT_ID = "tenant_id";
    public static final String TABLE_FIDO_LOG_COLUMN_USER_NAME = "user_name";
    public static final String TABLE_FIDO_LOG_COLUMN_REQUEST = "request";
    public static final String TABLE_FIDO_LOG_COLUMN_RESPONSE = "response";
    public static final String TABLE_FIDO_LOG_COLUMN_OPER_TYPE = "oper_type";
    public static final String TABLE_FIDO_LOG_COLUMN_OPER_ERROR = "oper_error";
    public static final String TABLE_FIDO_LOG_COLUMN_OPER_CODE = "oper_code";
    public static final String TABLE_FIDO_LOG_COLUMN_LOCAL_IP = "local_ip";
    public static final String TABLE_FIDO_LOG_COLUMN_REMOTE_IP = "remote_ip";
    public static final String TABLE_FIDO_LOG_COLUMN_CREATETS = "createts";
    public static final String TABLE_FIDO_LOG_COLUMN_FIELD1 = "field1";
    public static final String TABLE_FIDO_LOG_COLUMN_FIELD2 = "field2";

    public static final String TABLE_OPER_LOG = "t_oper_log";
    public static final String TABLE_OPER_LOG_COLUMN_OPR_ID = "opr_id";
    public static final String TABLE_OPER_LOG_COLUMN_USER_ID = "user_id";
    public static final String TABLE_OPER_LOG_COLUMN_USER_NAME = "user_name";
    public static final String TABLE_OPER_LOG_COLUMN_OPR_CONTENT = "opr_content";
    public static final String TABLE_OPER_LOG_COLUMN_ADDRESS = "address";
    public static final String TABLE_OPER_LOG_COLUMN_OPR_RESULT = "opr_result";
    public static final String TABLE_OPER_LOG_COLUMN_CAUSE = "cause";
    public static final String TABLE_OPER_LOG_COLUMN_CREATETS = "createts";

    /**************************** 此部分代码由主方法生成 *******************************/


    public static String getValueFromProperties(String key) {
        return PropKit.use("application.properties").get(key);
    }

    public static String getValueFromProperties(String properties, String key) {
        return PropKit.use(properties).get(key);
    }

    /**
     * 去除字符串最后一个逗号
     *
     * @param value
     * @return
     */
    public static String operLastSeparatorComma(String value) {
        if (StrKit.isBlank(value)) {
            return null;
        }
        String tmp = value.substring(0, value.lastIndexOf(SystemConst._DEFAULT_SEPARATOR_COMMA));
        return tmp;
    }

    /**
     * 根据传入的集合生成SQL语句IN中的内容
     *
     * @param values
     * @param sqlParams
     * @return
     */
    public static void assemblySqlForArray(String[] values, StringBuilder sqlParams) {

        for (int i = 0; i < values.length; i++) {
            sqlParams.append("'").append(values[i]).append("'");
            if (i != (values.length - 1)) {
                sqlParams.append(",");
            }
        }

        LOG.info("根据传入的集合生成SQL语句IN中的内容 ==> " + sqlParams.toString());
    }


    /**
     * 将传入的所有字符串进行拼装
     *
     * @param params
     * @return
     */
    public static String assemblySql(String... params) {
        if (params.length == 0) {
            return null;
        }

        StringBuilder sql = new StringBuilder();

        for (String str : params) {
            sql.append(str);
        }

        return sql.toString();
    }


    public static java.sql.Timestamp getDatabaseDateTime() {
        return new java.sql.Timestamp(new java.util.Date().getTime());
    }


    /**
     * 根据JNDI获取数据库连接
     *
     * @return
     */
    public static Connection getConnection() {
        DataSource dataSource = null;
        Connection conn = null;

        try {
            dataSource = (DataSource) new InitialContext().lookup("java:comp/env/" + SystemConst.DB_JNDI_NAME);
        } catch (NamingException e) {
            LOG.error("获取不到" + SystemConst.DB_JNDI_NAME + "的JNDI名称",e);
        }


        try {
            conn = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return conn;
    }
}
